package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cg.bean.CgMaterial;
import com.jy.adscm.operation.cg.mapper.mysql.CgMaterialMapper;
import com.jy.adscm.operation.mscyb.bean.*;
import com.jy.adscm.operation.mscyb.mapper.mysql.*;
import com.jy.adscm.operation.mscyb.service.JqzzMachineService;
import com.jy.adscm.operation.purchase.bean.Purchasebom;
import com.jy.adscm.operation.purchase.mapper.mysql.PurchasebomMapper;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.JyDrawingType;
import com.jy.adscm.zltx.mapper.mysql.JyDrawingTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-12-04
 */
@Service
public class JqzzMachineServiceImpl extends ServiceImpl<JqzzMachineMapper, JqzzMachine> implements JqzzMachineService {
   @Autowired
    JyUserMapper jyUserMapper;
   @Autowired
    JyCodeMapper jyCodeMapper;
   @Autowired
    JqzzSettingMapper jqzzSettingMapper;
    @Autowired
    JqzzSettingInputMapper jqzzSettingInputMapper;
    @Autowired
    JqzzSettingPartsMapper jqzzSettingPartsMapper;
    @Autowired
    JqzzSettingProcessMapper jqzzSettingProcessMapper;
    @Autowired
    CgMaterialMapper cgMaterialMapper;
    @Autowired
    JqzzMaterialMapper  jqzzMaterialMapper;
    @Autowired
    JqzzPartsMaterialMapper jqzzPartsMaterialMapper;
    @Autowired
    JqbzSettingsMapper jqbzSettingsMapper;
    @Autowired
    JqzzAssemblyMapper jqzzAssemblyMapper;
    @Autowired
    JyDrawingTypeMapper jyDrawingTypeMapper;
  @Transactional
    @Override
    public Boolean updateJqzzMachine(JqzzMachine jqzzMachine, Token token) {
        //待办修改
        if (jqzzMachine.getTaskstate()==0 && jqzzMachine.getTaskfinishstate()==0 &&
                "0".equals(jqzzMachine.getIstaskstart()) &&  "0".equals(jqzzMachine.getIstaskbegin()) &&
                "0".equals(jqzzMachine.getIstaskend())  && "0".equals(jqzzMachine.getIsreviewtask())
        ){

            return  super.updateById(jqzzMachine);
        }
        //接收任务
        if (jqzzMachine.getTaskstate()==0 && jqzzMachine.getTaskfinishstate()==0 &&
                "1".equals(jqzzMachine.getIstaskstart()) &&  "0".equals(jqzzMachine.getIstaskbegin()) &&
                "0".equals(jqzzMachine.getIstaskend())  && "0".equals(jqzzMachine.getIsreviewtask())
        ){
            jqzzMachine.setIstaskstart("1");
            jqzzMachine.setTaskstartaddtime(new Date());
            jqzzMachine.setTaskstate(1);
            return  super.updateById(jqzzMachine);
        }
        //开始任务
        if (jqzzMachine.getTaskstate()==1 && jqzzMachine.getTaskfinishstate()==0 &&
                "1".equals(jqzzMachine.getIstaskstart()) &&  "1".equals(jqzzMachine.getIstaskbegin()) &&
                "0".equals(jqzzMachine.getIstaskend())  && "0".equals(jqzzMachine.getIsreviewtask())
        ){
            jqzzMachine.setIstaskbegin("1");
            jqzzMachine.setTaskbeginaddtime(new Date());
            return super.updateById(jqzzMachine);
        }
        //结束任务
        if (jqzzMachine.getTaskstate()==1 && jqzzMachine.getTaskfinishstate()==0 &&
                "1".equals(jqzzMachine.getIstaskstart()) &&  "1".equals(jqzzMachine.getIstaskbegin()) &&
                "1".equals(jqzzMachine.getIstaskend())  && "0".equals(jqzzMachine.getIsreviewtask())
        ){

            JqzzMachine oldjqzzMachine = super.selectById(jqzzMachine.getTid());
            if ("0".equals(oldjqzzMachine.getIstaskend())){
                jqzzMachine.setIstaskend("1");
                jqzzMachine.setTaskendaddtime(new Date());
                jqzzMachine.setTaskfinishstate(1);
                //生成老化记录
                return super.updateById(jqzzMachine);
            }


        }
        //复核任务
        if (jqzzMachine.getTaskstate()==1 && jqzzMachine.getTaskfinishstate()==1 &&
                "1".equals(jqzzMachine.getIstaskstart()) &&  "1".equals(jqzzMachine.getIstaskbegin()) &&
                "1".equals(jqzzMachine.getIstaskend())  && "1".equals(jqzzMachine.getIsreviewtask())
        ){
            jqzzMachine.setReviewaddtime(new Date());
            if ("1".equals(jqzzMachine.getReviewresult())){
                jqzzMachine.setIsreviewtask("1");
            }
            jqzzMachine.setReviewperson(token.getUser().getId().toString());
            return super.updateById(jqzzMachine);
        }

        return null;
    }

    @Override
    public Page<JqzzMachine> selectJqzzMachine(Page<JqzzMachine> page , QueryJqzzMachine queryJqzzMachine,Token token) {
        Wrapper<JqzzMachine> wrapper =new EntityWrapper<>();
        wrapper.eq("TASKSTATE",queryJqzzMachine.getTaskState());
        wrapper.eq("TASKFINISHSTATE",queryJqzzMachine.getTaskFinishState());
        wrapper.eq("isReviewTask",queryJqzzMachine.getIsReviewTask());

        //判断是生产一部工作人员  215
        Boolean aBoolean = jyUserMapper.queryUserIsHasRole(token.getUser().getId().toString(), "215");
        if (aBoolean){
            wrapper.eq("TASKPERFORMPERSON",token.getUser().getId().toString());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqzzMachine.getCode(),null)){
            wrapper.like("TASKCODE",queryJqzzMachine.getCode());
        }

        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqzzMachine.getName(),null)){
            wrapper.eq("RESOURCENAME",queryJqzzMachine.getName());
        }
        page = super.selectPage(page, wrapper);
        List<JqzzMachine> jqzzMachines = page.getRecords();
        for (JqzzMachine jqzzMachine : jqzzMachines) {
            if ( jqzzMachine.getTaskreleaseperson()!=null && !"".equals( jqzzMachine.getTaskreleaseperson())){
                JyUser jyUser = jyUserMapper.selectById(jqzzMachine.getTaskreleaseperson());
                jqzzMachine.setTaskreleasepersonName(jyUser.getName());
            }
            if ( jqzzMachine.getTaskperformperson()!=null && !"".equals( jqzzMachine.getTaskperformperson())){
                JyUser jyUser = jyUserMapper.selectById(jqzzMachine.getTaskperformperson());
                jqzzMachine.setTaskperformpersonName(jyUser.getName());
            }

            if ( jqzzMachine.getReviewperson()!=null && !"".equals( jqzzMachine.getReviewperson())){
                JyUser jyUser = jyUserMapper.selectById(jqzzMachine.getReviewperson());
                jqzzMachine.setReviewpersonName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzMachine.getResourcename(),null)){

                JqzzSetting jqzzSetting = jqzzSettingMapper.selectById(jqzzMachine.getResourcename());
                jqzzMachine.setResourcenameName(jqzzSetting.getName());
                jqzzMachine.setMacType(Integer.valueOf(jqzzSetting.getType()));
                JyDrawingType jyDrawingType = jyDrawingTypeMapper.selectById(Integer.valueOf(jqzzSetting.getType()));
                jqzzMachine.setMacTypeName(jyDrawingType.getType());
            }
        }
        page.setTotal(selectCount(wrapper));
        return page;
        

    }

    @Override
    public List<JyUser> selectTaskPerformPerson() {
        List<JyUser> jyUsers = jyUserMapper.selectTaskPerformPerson("96");
        return jyUsers;
    }

    @Override
    public List<JyCode> selectMachine() {
        Wrapper<JyCode> wrapper= new EntityWrapper<>();
        wrapper.eq("code_type",2);
        List<JyCode> jyCodes = jyCodeMapper.selectList(wrapper);
        return jyCodes;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String insertJqzzMachine(JqzzMachine jqzzMachine) throws AdscmSystemException {
        String taskperformperson = jqzzMachine.getTaskperformperson();
        String code ="";
        if (AdscmBaseUtils.getInstance().isNotEmpty(jqzzMachine.getResourcename(),null)){
            //根据输入的机器类型 查找当前最大机器编号，然后生成最大的机器编号
            if (!AdscmBaseUtils.getInstance().isNotEmpty(jqzzMachine.getTaskstartorendtime(),null)){
             throw new AdscmSystemException("完成日期没得填写");
            }

            JyDrawingType jyCode = jyDrawingTypeMapper.selectById(jqzzMachine.getResourcename());
            String maccode = jyCode.getCode();
            //String yearMonth = new SimpleDateFormat("yyMMdd").format(jqzzMachine.getTaskstartorendtime());
            String yearMonth =jqzzMachine.getBatchnums() ;
            jqzzMachine.setBatchnums(yearMonth);
            Wrapper<JqzzMachine>  wrapper=new EntityWrapper<>();
            wrapper.like("TASKCODE", maccode + yearMonth, SqlLike.RIGHT);
            wrapper.orderBy("TASKCODE",false);
            List<JqzzMachine> jqzzMachines = super.selectList(wrapper);

            if (jqzzMachines.size()>0){
                JqzzMachine oldjqzzMachine = jqzzMachines.get(0);
                String oldCode = oldjqzzMachine.getTaskcode();
                 long tempCode=Long.valueOf(oldCode.replace(maccode,""))+1;
                code=maccode+String.valueOf(tempCode);
            }else {
                code=maccode+yearMonth+"01";
            }
           //根据机器类型号拿到对应的所有的总装配置
            Wrapper<JqzzSetting> jqzzMachineWrapper =new EntityWrapper<>();
            if ("3".equals(jqzzMachine.getResourcename())){
                //置换机  id 3 54 56
                jqzzMachineWrapper.eq("type",jqzzMachine.getResourcename());
            }
            if ("1".equals(jqzzMachine.getResourcename())){
                //单采机 4 54 55
                jqzzMachineWrapper.eq("type",jqzzMachine.getResourcename());
            }
            if ("2".equals(jqzzMachine.getResourcename())){
                //单采机-1 5 54 55
                jqzzMachineWrapper.eq("type",jqzzMachine.getResourcename());
            }



            List<JqzzSetting> jqzzSettings = jqzzSettingMapper.selectList(jqzzMachineWrapper);
            for (JqzzSetting jqzzSetting : jqzzSettings) {
                //创建任务
                jqzzMachine.setTaskperformperson(taskperformperson);
                jqzzMachine.setTaskreleaseaddtime(new Date());
                jqzzMachine.setTaskfinishstate(0);
                jqzzMachine.setTaskstate(0);
                jqzzMachine.setIstaskstart("0");
                jqzzMachine.setIstaskbegin("0");
                jqzzMachine.setIstaskend("0");
                jqzzMachine.setIsreviewtask("0");
                jqzzMachine.setReviewresult("0");
                jqzzMachine.setResourcename(jqzzSetting.getId().toString());
                jqzzMachine.setTaskcode(code);
                super.insert(jqzzMachine);
                //根据配置拿到他的零件物料输入列表
                Wrapper<JqzzSettingInput> getInputWrapper=new EntityWrapper<>();
                getInputWrapper.eq("pid",jqzzSetting.getId());
                List<JqzzSettingInput> jqzzSettingInputs = jqzzSettingInputMapper.selectList(getInputWrapper);
                for (JqzzSettingInput jqzzSettingInput : jqzzSettingInputs) {
                    JqzzMaterial jqzzMaterial=new JqzzMaterial();
                    CgMaterial cgMaterial = cgMaterialMapper.selectById(jqzzSettingInput.getCid());
                    jqzzMaterial.setTid(jqzzMachine.getTid());
                    jqzzMaterial.setName(jqzzSettingInput.getCid().toString());
                    jqzzMaterial.setCode(cgMaterial.getCode());
                    jqzzMaterial.setType(cgMaterial.getType());
                    jqzzMaterial.setIsAssembly(0);
                    jqzzMaterial.setNum(jqzzSettingInput.getNum());
                    jqzzMaterialMapper.insert(jqzzMaterial);
                }
                //创建部件的输入列表
                Wrapper<JqzzSettingParts> jqzzSettingPartsWrapper=new EntityWrapper<>();
                jqzzSettingPartsWrapper.eq("pid",jqzzSetting.getId());
                List<JqzzSettingParts> jqzzSettingParts = jqzzSettingPartsMapper.selectList(jqzzSettingPartsWrapper);
                for (JqzzSettingParts jqzzSettingPart : jqzzSettingParts) {
                    JqzzPartsMaterial  jqzzPartsMaterial =new JqzzPartsMaterial();
                    JqbzSettings jqbzSettings = jqbzSettingsMapper.selectById(jqzzSettingPart.getCid());
                    jqzzPartsMaterial.setTid(jqzzMachine.getTid());
                    jqzzPartsMaterial.setName(jqzzSettingPart.getCid().toString());
                    jqzzPartsMaterial.setType(jqbzSettings.getType());
                    jqzzPartsMaterial.setCode(jqbzSettings.getCode());
                    jqzzPartsMaterial.setIsAssembly(0);
                    jqzzPartsMaterial.setNum(jqzzSettingPart.getNum());
                    jqzzPartsMaterialMapper.insert(jqzzPartsMaterial);
                }
                //装配过程记录
                Wrapper<JqzzSettingProcess> jqzzSettingProcessWrapper =new EntityWrapper<>();
                jqzzSettingProcessWrapper.eq("pid",jqzzSetting.getId());
                List<JqzzSettingProcess> jqzzSettingProcesses = jqzzSettingProcessMapper.selectList(jqzzSettingProcessWrapper);
                for (JqzzSettingProcess jqzzSettingProcess : jqzzSettingProcesses) {
                    JqzzAssembly jqzzAssembly =new JqzzAssembly();
                    jqzzAssembly.setPid(jqzzMachine.getTid());
                    jqzzAssembly.setCid(jqzzSettingProcess.getId());
                    jqzzAssembly.setName(jqzzSettingProcess.getName());
                    jqzzAssembly.setReference(jqzzSettingProcess.getReference());
                    jqzzAssembly.setUnit(jqzzSettingProcess.getUnit());
                    jqzzAssembly.setType(jqzzSettingProcess.getType());
                    jqzzAssembly.setRank(jqzzSettingProcess.getRank());
                    jqzzAssembly.setRemark(jqzzSettingProcess.getRemark());
                    jqzzAssemblyMapper.insert(jqzzAssembly);
                }
            }

        }
        return code;
    }
}
